package cn.kgc.tangcco.dao.impl;

import cn.kgc.tangcco.dao.IdiomMongoDao;
import cn.kgc.tangcco.model.LayPage;
import cn.kgc.tangcco.pojo.Idiom;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/29 15:41
 */
@Component
public class IdiomMongoDaoImpl implements IdiomMongoDao {
    @Autowired
    private MongoOperations mongoOperations;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public List<Idiom> findAll() {
        return mongoTemplate.findAll(Idiom.class);
    }

    @Override
    public void saveAll(List<Idiom> idioms) {
        mongoTemplate.insertAll(idioms);
    }

    @Override
    public Long selectCount() {
        return mongoTemplate.count(new Query(), Idiom.class);
    }


    @Override
    public List<Idiom> select(int page, int limit) {
        LayPage<Idiom> layPage = new LayPage<>();
        Pageable pageable = PageRequest.of(page, limit);
        Query query = new Query();
        query.with(pageable);
        return mongoOperations.find(query, Idiom.class);
    }
}
